﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.User;

namespace Admin.User
{
    public partial class UserList : BaseCommon.BasePage
    {
        protected int pageSize = 10;
        protected int page = 1;
        protected int allCount = 0;
        protected string pageUrl = string.Empty;
        protected string strWhere = string.Empty;
        protected string name = string.Empty;//名称
        protected string userName = string.Empty;//名称
        protected string code = string.Empty; //代码
        protected UserBiz ddb = new UserBiz();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            this.InitData();
            string action = PageContext.GetRequest("action");
            if (!string.IsNullOrEmpty(action))
            {
                if (action.Trim().ToLower() == "stop")
                {
                    Delete();
                }
                else if (action.ToLower() == "download")
                {
                    this.DownLoad();
                }
                return;
            }
            this.BindData();
        }

        //删除数据
        protected bool Delete()
        {
            string strID = PageContext.GetRequest("ID");
            string succPageUrl = Server.UrlDecode(Request["pageurl"]);
            int ID = 0;
            int.TryParse(strID, out ID);
            if (ID == 0)
            {
                Jscript.AlertAndGoBack("参数错误");
                return false;
            }
            bool isSucc = ddb.Delete(ID, UserLogin);
            if (!isSucc)
            {
                Jscript.AlertAndGoBack("删除用户失败");
                return false;
            }
            Jscript.AlertAndRedirect("删除用户成功", succPageUrl);
            return true;
        }

        protected void InitData()
        {
            pageUrl = "/Admin/User/UserList.aspx";
            //当前页码
            if (PageContext.GetRequest("page") != string.Empty)
            {
                int.TryParse(PageContext.GetRequest("page"), out page);
            }
            pageUrl += "?page=" + page;
            //名称
            name = PageContext.GetRequest("txtName");
            if (name != string.Empty)
            {
                pageUrl += "&txtName=" + name;
                strWhere += " and sname like '%" + name + "%'";
            }
            userName = PageContext.GetRequest("txtUserName");
            if (userName != string.Empty)
            {
                pageUrl += "&txtUserName=" + userName;
                strWhere += " and UserName = '" + userName + "'";
            }
        }

        protected void BindData()
        {
            this.rptDataList.DataSource = ddb.GetList(strWhere, pageSize, page, ref allCount);
            this.rptDataList.DataBind();
            //设置分页参数
            this.pageCountrol.CurPage = page;
            this.pageCountrol.PageSize = pageSize;
            this.pageCountrol.AllCount = allCount;
            this.pageCountrol.PageUrl = pageUrl;
            this.pageCountrol.PageName = "page";
        }

        //下载报表
        protected void DownLoad()
        {
            string fileName = "用户信息" + DateTime.Now.ToString("yyyyMMdd");
            string[] titles = new string[] { "用户名", "姓名", "岗位", "部门", "邮箱" };
            string[] fields = new string[] { "UserName", "sName", "gangweiname", "departmentname", "eMail" };

            int tempAllCount = 0;
            DataTable dt = ddb.GetList("", 1000, 1, ref tempAllCount);
            if (dt == null)
            {
                return;
            }
           
            ToExcel(dt, fileName, titles, fields);
        }

    }
}